package com.sphd.beautysalon.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.utils.AppManager;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.R;
import com.wzh.frame.utils.BigDisMailUtils;
import com.wzh.frame.utils.SPUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Hu on 2016/10/25.
 */

public class BaseActivity extends AppCompatActivity {

    public Context context;
    private static final String PACKAGE_URL_SCHEME = "package:"; // 方案
    AlertDialog dialog;
    private View footView;
    private Dialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("BaseActivity", "BaseActivity");
        context = this;
        footView = View.inflate(context, R.layout.foot_view, null);
        loadingDialog = new Dialog(context, R.style.CustomProgressDialog);
        loadingDialog.setContentView(R.layout.common_dialog_loading_layout);
        loadingDialog.setCanceledOnTouchOutside(false);
    }

    public void addActivity(Activity activity) {
        AppManager.getAppManager().addActivity(activity);
    }

    public void finishActivity(Activity activity) {
        AppManager.getAppManager().finishActivity(activity);
    }

    public Object get(String key, Object defaultObject) {
        return SPUtils.get(context, key, defaultObject);
    }

    public void put(String key, Object defaultObject) {
        SPUtils.put(context, key, defaultObject);
    }

    public void remove(String key) {
        SPUtils.remove(context, key);
    }


    public void setTitleBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title.trim());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public void setTitleBarNoBack(Toolbar toolbar, String title) {
        toolbar.setTitle(title.trim());
        setSupportActionBar(toolbar);

    }

    public boolean isEmpty(TextView textView) {
        return TextUtils.isEmpty(textView.getText().toString().trim());
    }

    public void setRefresh(SwipeRefreshLayout myRefreshLayout) {
        myRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);
    }

    public void setRecy(RecyclerView recy) {

        recy.setHasFixedSize(true);
        recy.setLayoutManager(new LinearLayoutManager(context));
        recy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    public void setGRecy(RecyclerView recy, int i) {

        recy.setHasFixedSize(true);

        recy.setLayoutManager(new GridLayoutManager(context, i));

    }

    public boolean isCg_has_storemanager(RestaurantInfo restaurantInfo) {
        if (restaurantInfo.getStaff_status() == 1) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isSuperManager(LoginInfo loginInfo) {
        if (loginInfo.getStaff_auth() == 2) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isManager(LoginInfo loginInfo) {
        if (loginInfo.getStaff_auth() == 3) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isReciver(LoginInfo loginInfo) {
        if (loginInfo.getStaff_auth() == 4) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isWaiter(LoginInfo loginInfo) {
        if (loginInfo.getStaff_auth() == 5) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isFundinger(LoginInfo loginInfo) {
        if (loginInfo.getStaff_auth() == 6) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isNoHeard(String content) {
        Pattern pattern = Pattern.compile("^(\\d+)(.*)");
        Matcher matcher = pattern.matcher(content);
        return matcher.matches();
    }

    public String turn(Object val) {
        return BigDisMailUtils.turnTrim(val, Constant.DECIMAL_POINT);
    }

    public String turn(Object val, int i) {
        return BigDisMailUtils.turnTrim(val, i);
    }

    public void addFootView(BaseQuickAdapter baseQuickAdapter) {
//        footView.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        baseQuickAdapter.removeAllFooterView();
        baseQuickAdapter.addFooterView(footView);
    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent();
        intent.putExtra(Constant.TAG, context.getClass().getSimpleName());
        intent.setClass(context, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.putExtra(Constant.TAG, context.getClass().getSimpleName());
        intent.setClass(context, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, Intent intent) {
        intent.setClass(context, cls);
        intent.putExtra(Constant.TAG, context.getClass().getSimpleName());
        startActivity(intent);
    }

    public void toast(Object o) {
        if (o instanceof Integer) {
            Toast.makeText(context, (Integer) o, Toast.LENGTH_SHORT).show();
        } else if (o instanceof CharSequence) {
            Toast.makeText(context, (CharSequence) o, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, o + "", Toast.LENGTH_SHORT).show();
        }

    }

    // 启动应用的设置
    public void startAppSettings() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
        startActivity(intent);
    }

    public void setImage(ImageView image, String url) {
        ImageLoader imageLoader = RestaurantsApplication.getImageLoader();
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(image,
                R.mipmap.p, R.mipmap.p);
        imageLoader.get(RestaurantUrl.Httpimg + url, listener);
    }

    public void setImageOut(ImageView image, String url) {
        ImageLoader imageLoader = RestaurantsApplication.getImageLoader();
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(image,
                R.mipmap.moren, R.mipmap.moren);
        imageLoader.get(url, listener);
    }

    public View setDialog(int layoutId) {
        View view = View.inflate(context, layoutId, null);
        dialog = new AlertDialog.Builder(this).setView(view)
                //在这里把写好的这个listview的布局加载dialog中
                .create();
//        dialog.setCanceledOnTouchOutside(false);//使除了dialog以外的地方不能被点击

        return view;
    }

    public AlertDialog setAlertDialog(View view) {
        final AlertDialog dialog = new AlertDialog.Builder(context).setView(view)//在这里把写好的这个listview的布局加载dialog中
                .create();
        dialog.setCanceledOnTouchOutside(false);//使除了dialog以外的地方不能被点击

        return dialog;
    }


    public View getView(int layoutId) {
        View view = View.inflate(context, layoutId, null);
        return view;
    }

    public void showResesDialog() {
        dialog.show();
    }

    public boolean isNumber1(String str) {// 判断整型
        return str.matches("^\\d+$$");
    }

    public <T> T $(int viewID, View heardView) {
        return (T) heardView.findViewById(viewID);
    }

    public static boolean isNum(String str) {
        return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }
    public boolean isManagerCanDo(LoginInfo loginInfo, RestaurantInfo restaurantInfo) {
        if (!isManager(loginInfo)) {
            if (isSuperManager(loginInfo)) {
                if (!isCg_has_storemanager(restaurantInfo)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return true;
        }

    }

    public boolean isReciverCanDo(LoginInfo loginInfo, RestaurantInfo restaurantInfo) {
        if (!isReciver(loginInfo)) {
            if (isSuperManager(loginInfo)) {
                if (!isCg_has_storemanager(restaurantInfo)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return true;
        }

    }

    public boolean isWaiterCanDo(LoginInfo loginInfo, RestaurantInfo restaurantInfo) {
        if (!isWaiter(loginInfo)) {
            if (isSuperManager(loginInfo)) {
                if (!isCg_has_storemanager(restaurantInfo)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return true;
        }

    }

    public void showLoadingDialog() {
        if (loadingDialog != null && !loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    public void dissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }


}
